/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#include "EntityMessages.hpp"

namespace XernProtocol
{
	// EntityMovementUpdates
	EntityMovementUpdates::EntityMovementUpdates()
	{
		time = 0;
	}

	EntityMovementUpdates::~EntityMovementUpdates()
	{
	}

	MessageType EntityMovementUpdates::GetType() const
	{
		return MT_ENTITY_MOVEMENT_UPDATES;
	}

	size_t EntityMovementUpdates::GetSize()
	{
		return Update::UpdateSize*updates.size() + 9;
	}

	void EntityMovementUpdates::Decode(DataStream &in, size_t size)
	{
		// Read the time.
		in >> time;

		// Read the number of updates.
		u8 numupdates;
		in >> numupdates;

		// Decode the updates
		updates.resize(numupdates);
		for(int i = 0; i < numupdates; i++)
		{
			Update &up = updates[i];
			in >> up.id >> up.parentId;
			in >> up.position.x >> up.position.y >> up.position.z;
			in >> up.velocity.x >> up.velocity.y >> up.velocity.z;
			in >> up.acceleration.x >> up.acceleration.y >> up.acceleration.z;
			in >> up.rotation.x >> up.rotation.y >> up.rotation.z;
			in >> up.angularVelocity.x >> up.angularVelocity.y >> up.angularVelocity.z;

			// Decompress the rotation.
			float xyzsum = 1.0f - up.rotation.x*up.rotation.x - up.rotation.y*up.rotation.y - up.rotation.z*up.rotation.z;
			if(xyzsum > 0.0)
				up.rotation.w = squareroot(xyzsum);
		}
	}

	void EntityMovementUpdates::Encode(DataStream &out) const
	{
		// Write the time.
		out << time;

		// Write the number of updates.
		u8 numupdates = updates.size();
		out << numupdates;

		// Encode the updates.
		for(int i = 0; i < numupdates; i++)
		{
			Update &up = updates[i];

			// Compress the rotation
			float length = up.rotation.Length();
			if(CompareFloat(length, 0.0))
			{
				if(up.rotation.w < 0.0f)
					length = -1.0f/length;
				else
					length = 1.0f/length;
				up.rotation = up.rotation*length;
			}

			out << up.id << up.parentId;
			out << up.position.x << up.position.y << up.position.z;
			out << up.velocity.x << up.velocity.y << up.velocity.z;
			out << up.acceleration.x << up.acceleration.y << up.acceleration.z;
			out << up.rotation.x << up.rotation.y << up.rotation.z;
			out << up.angularVelocity.x << up.angularVelocity.y << up.angularVelocity.z;
		}
	}

	// EntityPhysicUpdate
	EntityPhysicUpdates::EntityPhysicUpdates()
	{
	}

	EntityPhysicUpdates::~EntityPhysicUpdates()
	{
	}

	MessageType EntityPhysicUpdates::GetType() const
	{
		return MT_ENTITY_PHYSIC_UPDATES;
	}

	size_t EntityPhysicUpdates::GetSize()
	{
		return 9 + updates.size()*Update::UpdateSize;
	}

	void EntityPhysicUpdates::Decode(DataStream &in, size_t size)
	{
		u8 numupdates;
		in >> time >> numupdates;
		updates.reserve(numupdates);
		for(size_t i = 0; i < numupdates; i++)
		{
			Update up;
			in >> up.physicType >> up.mass >> up.entity >> up.collisionShape;
			in >> up.linearFactor.x >> up.linearFactor.y >> up.linearFactor.z;
			in >> up.angularFactor.x >> up.angularFactor.y >> up.angularFactor.z;
			in >> up.linearSleepThreshold >> up.angularSleepThreshold;
			updates.push_back(up);
		}
	}

	void EntityPhysicUpdates::Encode(DataStream &out) const
	{
		u8 numupdates = updates.size();
		out << time << numupdates;
		for(size_t i = 0; i < numupdates; i++)
		{
			const Update &up = updates[i];
			out << up.physicType << up.mass << up.entity << up.collisionShape;
			out << up.linearFactor.x << up.linearFactor.y << up.linearFactor.z;
			out << up.angularFactor.x << up.angularFactor.y << up.angularFactor.z;
			out << up.linearSleepThreshold << up.angularSleepThreshold;
		}
	}

	// EntityAdded
	EntityAdded::EntityAdded()
	{
		time = 0;
	}

	EntityAdded::~EntityAdded()
	{
	}

	MessageType EntityAdded::GetType() const
	{
		return MT_ENTITY_ADDED;
	}

	size_t EntityAdded::GetSize()
	{
		return 9 + Data::DataSize*entityData.size();
	}

	void EntityAdded::Decode(DataStream &in, size_t size)
	{
		u8 numentities;
		in >> time >> numentities;

		entityData.reserve(numentities);
		for(int i = 0; i < numentities; i++)
		{
			Data data;
			in >> data.parentId >> data.id;
			entityData.push_back(data);
		}
	}

	void EntityAdded::Encode(DataStream &out) const
	{
		u8 numentities = entityData.size();
		out << time << numentities;

		for(int i = 0; i < numentities; i++)
		{
			const Data &data = entityData[i];
			out << data.parentId << data.id;
		}
	}

	// EntityRemoved
	EntityRemoved::EntityRemoved()
	{
		time = 0;
	}

	EntityRemoved::~EntityRemoved()
	{
	}

	MessageType EntityRemoved::GetType() const
	{
		return MT_ENTITY_REMOVED;
	}

	size_t EntityRemoved::GetSize()
	{
		return 9 + 16*entities.size();
	}

	void EntityRemoved::Decode(DataStream &in, size_t size)
	{
		u8 numentities;
		in >> time >> numentities;

		entities.resize(numentities);
		for(int i = 0; i < numentities; i++)
		{
			uuid id;
			in >> id;
			entities[i] = id;
		}
	}

	void EntityRemoved::Encode(DataStream &out) const
	{
		u8 numentities = entities.size();
		out << time << numentities;

		for(int i = 0; i < numentities; i++)
		{
			uuid id = entities[i];
			out << id;
		}
	}

	// EntityAddedResource
	EntityAddedResource::EntityAddedResource()
	{
		resourceFlags = 0;
	}

	EntityAddedResource::~EntityAddedResource()
	{
	}

	MessageType EntityAddedResource::GetType() const
	{
		return MT_ENTITY_ADDED_RESOURCE;
	}

	size_t EntityAddedResource::GetSize()
	{
		return Size + resourceMime.size() + resourceExtension.size();
	}

	void EntityAddedResource::Decode(DataStream &in, size_t size)
	{
		in >> entityId;
		in >> resourceId;
		in >> resourceHash;
		in >> resourceFlags;
		in >> resourceMime;
		in >> resourceExtension;
	}

	void EntityAddedResource::Encode(DataStream &out) const
	{
		out << entityId;
		out << resourceId;
		out << resourceHash;
		out << resourceFlags;
		out << resourceMime;
		out << resourceExtension;
	}

	// EntityRemovedResource
	EntityRemovedResource::EntityRemovedResource()
	{
	}

	EntityRemovedResource::~EntityRemovedResource()
	{
	}

	MessageType EntityRemovedResource::GetType() const
	{
		return MT_ENTITY_REMOVED_RESOURCE;
	}

	size_t EntityRemovedResource::GetSize()
	{
		return Size;
	}

	void EntityRemovedResource::Decode(DataStream &in, size_t size)
	{
		in >> entityId >> resourceId;
	}

	void EntityRemovedResource::Encode(DataStream &out) const
	{
		out << entityId << resourceId;
	}
}; // namespace XernProtocol
